package com.tanhua.manage.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.manage.domain.OperLog;
import com.tanhua.manage.service.FreezeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 冻结和解冻用户
 * @Author LianQingSen
 */
@RestController
@RequestMapping("/manage/users")
@Slf4j
public class FreezeController {

    @Autowired
    private FreezeService freezeService;

    /**
     * 冻结用户
     * @param map
     * @return
     */
    @OperLog(operModul = "冻结模块",operType = "冻结用户")
    @PostMapping("freeze")
    public ResponseEntity<Object> freezeUser(@RequestBody Map<String,String> map) {
        try {
            Integer userId = Convert.toInt(map.get("userId"));
            Integer freezingTime = Convert.toInt(map.get("freezingTime"));
            Integer freezingRange = Convert.toInt(map.get("freezingRange"));
            String reasonsForFreezing = Convert.toStr(map.get("reasonsForFreezing"));
            String frozenRemarks = Convert.toStr(map.get("frozenRemarks"));
            String message = freezeService.freezeUser(userId,freezingTime,freezingRange,reasonsForFreezing,frozenRemarks);
            if (ObjectUtil.isNotEmpty(message)) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            log.error("冻结用户失败~ ", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户解冻模块
     * @param map
     * @return
     */
    @OperLog(operModul = "冻结模块",operType = "解冻用户")
    @PostMapping("unfreeze")
    public ResponseEntity<Object> unfreezeUser(@RequestBody Map<String,String> map) {
        try {
            Integer userId = Convert.toInt(map.get("userId"));
            String reasonsForThawing = map.get("frozenRemarks");
            String message = freezeService.unfreezeUser(userId,reasonsForThawing);
            if (ObjectUtil.isNotEmpty(message)) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            log.error("用户解冻失败~ ", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
